import remoteSearchProductInput from './components/remote-search-product-input';
import remoteSearchEmpInput from './components/remote-search-emp-input';
import remoteSearchBatchCodeInput from './components/remote-search-batch-code-input';
import {
	spinSpecSheetAudit, spinSpecSheetClearAudit, spinSpecSheetClearClose,
	spinSpecSheetClearSubmitAudit, spinSpecSheetClose,
	spinSpecSheetCreate,
	spinSpecSheetCreateAudit,
	spinSpecSheetGet,
	spinSpecSheetUpdate,
	spinSpecSheetUpdateAudit
} from '../../../../api/technology/order';
import { spinProcessPage } from '../../../../api/technology/process';
import { spinMachineModelPage } from '../../../../api/basicData/machineModel';
import { spinSpecParamPage } from '../../../../api/technology/item';
import operationBarMethod from './components/operation-bar/operationBarMethed';
import handlerCache from '../../../../mixins/handler-cache';
export default {
	components: { remoteSearchProductInput, remoteSearchEmpInput, remoteSearchBatchCodeInput },
	mixins: [operationBarMethod, handlerCache],
	data() {
		return {
			prefix: 'spin:specSheet',
			apiItem: {
				'save': spinSpecSheetCreate,
				'saveAudit': spinSpecSheetCreateAudit,
				'update': spinSpecSheetUpdate,
				'updateAudit': spinSpecSheetUpdateAudit,
				'submit': spinSpecSheetUpdate, // 后端暂时没有'保存并提交接口'，先用update代替
				'cancelSubmit': spinSpecSheetClearSubmitAudit,
				'submitAudit': spinSpecSheetUpdateAudit,
				'audit': spinSpecSheetAudit,
				'cancelAudit': spinSpecSheetClearAudit,
				'close': spinSpecSheetClose,
				'cancelClose': spinSpecSheetClearClose
			},
			tableData: [{}],
			tableLoading: false,
			processList: [],
			updateAuditLoading: false,
			saveAuditLoading: false,
			saveBtnLoading: false,
			workshopList: [],
			showLoading: true,
			saveDialogState: false,
			machineModelList: [],
			ruleForm: {
				code: '系统自动生成',
				'batchCode': '',
				'dailyYield': null,
				'efficiencyPercent': null,
				'fixedLength': null,
				'gramWeight': null,
				'machineModelCode': '',
				'machineModelId': null,
				'machineModelName': '',
				'meters': null,
				'moistureRegain': null,
				'numbers': null,
				'processCode': '',
				'processId': null,
				'processName': '',
				'productCode': '',
				'productId': null,
				'productModels': '',
				'productName': '',
				'specDate': '',
				'specEmpId': null,
				'specEmpName': ''
			},
			rules: {
				meters: [{ required: true, trigger: 'change' }],
				moistureRegain: [{ required: true, trigger: 'change' }],
				gramWeight: [{ required: true, trigger: 'change' }],
				fixedLength: [{ required: true, trigger: 'change' }],
				efficiencyPercent: [{ required: true, trigger: 'change' }],
				dailyYield: [{ required: true, trigger: 'change' }],
				specEmpId: [{ required: true, trigger: 'change' }],
				productId: [{ required: true, trigger: 'change' }],
				machineModelId: [{ required: true, trigger: 'change' }],
				batchCode: [{ required: true, trigger: 'change' }],
				processId: [{ required: true, trigger: 'change' }],
				code: [{ required: true, trigger: 'change' }],
				name: [{ required: true, trigger: 'change' }]
			}
		};
	},
	methods: {
		setDependentDataOfForm() {
			const params = JSON.parse(JSON.stringify(this.ruleForm));
			params.processCode = this.processList.find(x => x.id === params.processId).code;
			params.specSheetParamList = JSON.parse(JSON.stringify(this.tableData));
			return params;
		},
		onProcessChange() {
			this.getTechnologyItemListRequest();
		},
		onSelectBatchCodeChange(e) {
			if (Object.keys(e).length) {
				this.$set(this.ruleForm, 'batchCode', e.batchCode);
			}
		},
		onSelectEmpChange(e) {
			if (Object.keys(e).length) {
				this.$set(this.ruleForm, 'specEmpId', e.id);
				this.$set(this.ruleForm, 'specEmpCode', e.code);
				this.$set(this.ruleForm, 'specEmpName', e.name);
			}
		},
		onSelectProductChange(e) {
			if (Object.keys(e).length) {
				this.setProductDependentData(e);
			}
		},
		setProductDependentData(e) {
			this.$set(this.ruleForm, 'productId', e.id);
			this.$set(this.ruleForm, 'productCode', e.code);
			this.$set(this.ruleForm, 'productName', e.name);
			this.$set(this.ruleForm, 'productModels', e.models);
		},
		defaultRowData() {
			return {
				processId: null,
				processCode: '',
				processName: ''
			};
		},
		getParamData() {
			const params = JSON.parse(JSON.stringify(this.ruleForm));
			params.processCode = this.processList.find(x => x.id === params.processId).code;
			params.processName = this.processList.find(x => x.id === params.processId).name;
			params.specSheetParamList = JSON.parse(JSON.stringify(this.tableData));
			return params;
		},
		saveAndAuditRequest() {
			this.saveAuditLoading = true;
			this.ruleForm.id = null;
			spinSpecSheetCreateAudit(this.getParamData()).then(res => {
				this.saveAuditLoading = false;
				if (res.data.status === 200) {
					if (res.data.status === 200) {
						this.$tipsBar(this, 'operation');
						this.$emit('on-finish');
					}
				}
			});
		},
		onConfirmBtn(type) {
			this.$refs['ruleForm'].validate(valid => {
				if (valid) {
					if (type === 'save') {
						this.createAndUpdateRequest();
					} else if (type === 'saveAndAudit') {
						this.saveAndAuditRequest();
					} else if (type === 'updateAndAudit') {
						this.updateAndAuditRequest();
					}
				} else {
					this.$tipsBar(this, 'validator');
				}
			});
		},
		createAndUpdateRequest() {
			this.saveBtnLoading = true;
			// 新增
			if (!this.dataId) {
				this.ruleForm.id = null;
				spinSpecSheetCreate(this.getParamData()).then(res => {
					this.saveBtnLoading = false;
					if (res.data.status === 200) {
						this.$tipsBar(this, 'save');
						this.$emit('on-finish');
					}
				});
				return;
			}
			// 编辑
			spinSpecSheetUpdate(this.getParamData()).then(res => {
				this.saveBtnLoading = false;
				if (res.data.status === 200) {
					this.$tipsBar(this, 'save');
					this.$emit('on-finish');
				}
			});
		},
		updateAndAuditRequest() {
			this.updateAuditLoading = true;
			spinSpecSheetUpdateAudit(this.getParamData()).then(res => {
				this.updateAuditLoading = false;
				if (res.data.status === 200) {
					if (res.data.status === 200) {
						this.$tipsBar(this, 'operation');
						this.$emit('on-finish');
					}
				}
			});
		},
		onVisibleChange(e) {
			this.$emit('on-visible-change', e);
			if (!e) {
				Object.assign(this.$data, this.$options.data());
				this.$nextTick(() => {
					this.$refs.ruleForm.resetFields();
				});
			}
		},
		getDetailRequest() {
			return spinSpecSheetGet({ id: this.dataId }).then(res => {
				this.showLoading = false;
				if (res.data.status === 200) {
					const resData = JSON.parse(JSON.stringify(res.data.res));
					delete resData.specSheetParamList;
					this.ruleForm = resData;
					this.tableData = res.data.res.specSheetParamList.map(x => {
						x.val = x.dataType === 1 ? Number(x.val) : x.val;
						x.valMax = x.dataType === 1 ? Number(x.valMax) : x.valMax;
						x.valMin = x.dataType === 1 ? Number(x.valMin) : x.valMin;
						return x;
					});
				}
			});
		},
		getProcessListRequest() {
			return spinProcessPage().then(res => {
				if (res.data.status === 200) {
					this.processList = res.data.res;
				}
			});
		},
		getMachineModelListRequest() {
			return spinMachineModelPage({ auditState: 1 }).then(res => {
				if (res.data.status === 200) {
					this.machineModelList = res.data.res;
				}
			});
		},
		getTechnologyItemListRequest() {
			if (!this.ruleForm.processId) return;
			spinSpecParamPage({ auditState: 1, processId: this.ruleForm.processId }).then(res => {
				if (res.data.status === 200) {
					this.tableData = res.data.res.map(x => {
						return {
							dataType: x.dataType,
							specParamId: x.id,
							ifBusi: x.ifBusi,
							ifNull: x.ifNull,
							ifWarning: x.ifWarning,
							iotIdentifier: x.iotIdentifier,
							specParamCode: x.code,
							specParamName: x.name,
							paramType: x.paramType,
							val: null,
							valMax: null,
							valMin: null
						};
					});
				}
			});
		},
		onJump() {
			this.$store.dispatch('tagsView/delView', { path: this.$route.path });
			this.$toMainRouter({
				name: 'TechnologyOrder'
			});
		},
		clearCacheMethod() {
			// Object.assign(this.$data, this.$options.data());
			this.$nextTick(() => {
				this.$refs.ruleForm.clearValidate();
				this.getDependentData();
			});
		}
	}
};
